#!/bin/bash
###################################
# $Id: backup 379 2012-04-02 08:43:42Z netkiller $
# Author: netkiller@msn.com
# Home:	http://netkiller.github.com
###################################
BACKUP_HOST=""
BACKUP_USER=""
BACKUP_PASS=""
BACKUP_DIR=/backup/www
#Number of copies
COPIES=30
####################################
LOGFILE='/var/tmp/test.log'
TAR='tar'
RSYNC="rsync"
WEEK=$(date -u +%A)
# date -u +%a
# date -u +%w
TIMEPOINT=$(date -u +%Y-%m-%d)
RSYNC_OPTS="-auz --delete --log-file=$LOGFILE"
TAR_OPTS="zcfP"
####################################
test ! -w $BACKUP_DIR && echo "Error: $BACKUP_DIR is un-writeable." && exit 0

umask 0077

for domain in $(ls -1 /www)
do
	test ! -d "$BACKUP_DIR/$domain" && mkdir -p "$BACKUP_DIR/$domain"
	echo "$domain:"
	for host in $(ls -1 /www/$domain)
	do
		echo " - $host"
		# Rsync
		backup_dir=$BACKUP_DIR/$domain/$host/$WEEK
		mkdir -p ${backup_dir}
 		$RSYNC $RSYNC_OPTS /www/$domain/$host/* $backup_dir
 		# TAR
		$TAR $TAR_OPTS $BACKUP_DIR/$domain/$host/$host.$TIMEPOINT.tgz /www/$domain/$host
	done
 	#$RSYNC $RSYNC_OPTS /www/$domain $BACKUP_DIR/$domain/$TIMEPOINT
	#$TAR $TAR_OPTS $BACKUP_DIR/$domain.$TIMEPOINT.tgz /www/$domain
done
find $BACKUP_DIR -type f -mtime +$COPIES -delete
